define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'check/order/index',
                    add_url: 'check/order/add',
                    edit_url: 'check/order/edit',
                    del_url: 'check/order/del',
                    multi_url: 'check/order/multi',
                    table: 'order',
                }
            });

            var table = $("#table");
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-audit").data("area", ["90%", "90%"]);
                $(".btn-detail").data("area", ["90%", "90%"]);
                $(".btn-service").data("area", ["90%", "90%"]);
                $(".btn-assign").data("area", ["50%", "50%"]);
                $(".btn-d4").data("area", ["80%", "80%"]);
                $(".btn-d5").data("area", ["80%", "80%"]);
            });
            var check_id = [];
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                maintainSelected: 1,
                onCheck: function (row, tr, flied) {
                    check_id.push(row.id)
                    //  console.log(row);
                },
                columns: [
                    [
                        {
                            checkbox: true, formatter: function (value, row, index) {
                                //                                        if (row.check == 1) {
                                //                                            return { checked: true };
                                //                                        }

                                if (check_id.includes(row.id)) {
                                    return {checked: true};
                                }
                            }
                        },
                        {field: 'lifts.lift_number', title: __('电梯编号'), operate: 'like'},
                        {field: 'order_id', title: __('订单编号'), operate: 'like'},
                        {field: 'specifications', title: __('电梯规格'), operate: false},
                        {field: 'lifts.real_name', title: '业主姓名', operate: 'like'},
                        {field: 'lifts.phone', title: '联系电话', operate: 'like'},
                        {field: 'lifts.area', title: '所在地区', operate: false},
                        {
                            field: 'address1',  title: '所在地区', searchList: function (column) {
                                return Template('address1', {});
                            }, formatter: function (value, row, index) {
                                return '无';
                            }, visible: false
                        },
                        {field: 'lifts.specific_address', title: '电梯地址', operate: 'like'},
                        // {field: 'lift.specific_address', title: '电梯地址', operate: 'like'},
                        {field: 'pay_price', title: '成交价格(￥)', operate: false},
                        {field: 'package', title: '套餐名称', operate: false},
                        {
                            field: 'package_name',  title: '套餐名称', searchList: function (column) {
                                return Template('package_name', {});
                            }, formatter: function (value, row, index) {
                                return '无';
                            }, visible: false
                        },
                        {field: 'duration', title: '套餐时长', operate: false},
                        {
                            field: 'duration_name',  title: '套餐时长', searchList: function (column) {
                                return Template('duration_name', {});
                            }, formatter: function (value, row, index) {
                                return '无';
                            }, visible: false
                        },
                        {
                            field: 'add_time',
                            title: __('创建时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {
                            field: 'complete_time',
                            title: __('结束时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: false,
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {
                            field: 'status', title: __('订单状态'),
                            searchList: {
                                //                                "1": __('待助力'),
                                "-2": __('待付款'),
                                "12": __('服务中'),
                                "13": __('服务完成'),
                                "14": __('已取消'),
                                "-1": __('售后'),
                            },
                            formatter: function (value, row) {
                                if (row.paid == 0) {
                                    if (row.status == 14) {
                                        return '<span style="">已取消</span>';
                                    } else {
                                        return '<span style="">待支付</span>';
                                    }
                                } else {
                                    if (row.status == 14) {
                                        return '已取消';
                                    } else if (row.status == 13) {
                                        return '服务完成';
                                    } else if (row.status == 12) {
                                        return '服务中';
                                    } else if (row.status == -1 || row.status == -2) {
                                        return '售后';
                                    }
                                }
                            }
                        },
                        {field: 'personnel', title: '业务员', operate: false},
                        {
                            field: 'personnel_id', title: '业务员', addclass: 'selectpage',
                            extend: 'data-source="check/free_clean/getSaleMan" data-field="title"', visible: false
                        },

                        {
                            field: 'operate', title: __('Operate'), table: table, buttons: [
                                {
                                    name: 'detail',
                                    text: __('查看详情'),
                                    title: __('查看详情'),
                                    classname: 'btn btn-xs btn-success btn-detail btn-magic btn-dialog',
                                    icon: 'fa fa-eye',
                                    url: 'check/order/detail?id={id}',
                                    // extend:'data-area=["60%","70%"]',
                                    callback: function (data) {
                                    },
                                },
                                {
                                    name: 'postpone',
                                    text: __('申请延期'),
                                    title: __('申请延期'),
                                    classname: 'btn btn-xs btn-warning btn-postpone btn-magic btn-dialog',
                                    icon: 'fa fa-paint-brush',
                                    url: 'check/order/postpone?id={id}',
                                    // extend:'data-area=["60%","70%"]',
                                    callback: function (data) {
                                    },
                                },
                                {
                                    name: 'assign',
                                    text: __('指派业务员'),
                                    title: __('指派业务员'),
                                    classname: 'btn btn-xs btn-danger btn-assign btn-magic btn-dialog',
                                    icon: 'fa fa-paint-brush',
                                    url: 'check/order/do_assign?id={id}',
                                    // extend:'data-area=["60%","70%"]',
                                    callback: function (data) {
                                    },
                                },
                                {
                                    name: 'see_service_order',
                                    text: __('查看服务单'),
                                    title: __('查看服务单'),
                                    classname: 'btn btn-xs btn-success btn-service btn-magic btn-dialog',
                                    icon: 'fa fa-eye',
                                    url: 'check/order/see_service_order?id={id}&&order_id={order_id}',
                                    // extend:'data-area=["60%","70%"]',
                                    callback: function (data) {
                                    },
                                },
                                {
                                    name: 'after_sale',
                                    text: __('申请售后'),
                                    title: __('申请售后'),
                                    classname: 'btn btn-xs btn-danger btn-assign btn-magic btn-dialog',
                                    icon: 'fa fa-paint-brush',
                                    url: 'check/order/after_sale?id={id}',
                                    // extend:'data-area=["60%","70%"]',
                                    callback: function (data) {
                                    },
                                },
                            ], events: Table.api.events.operate, formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                if (row.status == 3) {
                                    $(table).data("operate-audit", false); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                } else {
                                    $(table).data("operate-audit", true); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                }
                                $(table).data("operate-detail", false);
                                $(table).data("operate-assign", false);
                                $(table).data("operate-after_sale", false);
                                $(table).data("operate-see_service_order", false);
                                $(table).data("operate-postpone", false);

                                if (row.paid == 1) {
                                    if(row.status == 14){  //已取消
                                        $(table).data("operate-detail", true);
                                        $(table).data("operate-assign", true);
                                    }else if(row.status == 12){ //服务中
                                        $(table).data("operate-detail", true);
                                        $(table).data("operate-assign", true);
                                        $(table).data("operate-after_sale", true);
                                        $(table).data("operate-see_service_order", true);
                                        $(table).data("operate-postpone", true);
                                    }else if(row.status == 13){ //服务完成
                                        $(table).data("operate-detail", true);
                                        $(table).data("operate-assign", true);
                                        $(table).data("operate-see_service_order", true);
                                    }else if(row.status == -1 || row.status == -2){ //售后
                                        $(table).data("operate-detail", true);
                                        $(table).data("operate-assign", true);
                                        $(table).data("operate-see_service_order", true);
                                    }

                                } else {
                                    $(table).data("operate-detail", true);
                                    $(table).data("operate-assign", true);
                                }
                                $(table).data("operate-edit", null); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                $(table).data("operate-del", null); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        },
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            $(document).on("click", ".btn-most-assign", function () {
                var id = $(this).attr('val');
                console.log(check_id.join(","));
                //var nick_name=$(this).attr('nickname');
                Fast.api.open('check/order/do_most_assign?check_id=' + check_id.join(",") + '', __('批量派送'), {area: ['50%', '50%']}, {
                    callback: function (value) {

                    }
                });
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        do_most_assign: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            Controller.api.bindevent();
            $(document).on("click", ".see_service", function () {
                var id = $('#id').val();
                var order_id = $('#order_id').val();
                console.log(id);
                console.log(order_id);
                //var nick_name=$(this).attr('nickname');
                Fast.api.open('check/order/see_service_order?order_id=' + order_id + '&id='+id, __('查看服务单'), {area: ['90%', '90%']}, {
                    callback: function (value) {

                    }
                });
            });
        },
        //申请延期
        postpone: function () {
            Controller.api.bindevent();
        },
        //审核
        audit: function () {
            Controller.api.bindevent();
            let audit_status1 = document.getElementById('row[audit_status]-1')
            let audit_status2 = document.getElementById('row[audit_status]-0')

            //监听是开票的情况
            audit_status1.addEventListener('click', function () {
                $('.bh').hide();
            });

            //监听不是开票的情况
            audit_status2.addEventListener('click', function () {
                $('.bh').show();
            });
        },
        //指派业务员
        do_assign: function () {
            Controller.api.bindevent();
            var is_have_saleman = Config.is_have_saleman;
            if (is_have_saleman == 1) {
                $('.saleman').show();
            } else {
                $('.saleman').hide();
            }
        },
        //申请售后
        after_sale: function () {
            Controller.api.bindevent();
        },

        see_service_order: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'check/order/see_service_order' + location.search,
                    add_url: 'train/train/add_content',
                    edit_url: 'train/train/edit_content',
                    del_url: 'train/train/del_content',
                    multi_url: 'train/train/multi',
                    table: 'content',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'service_id', title: __('服务单编号'), sortable: true},
                        {field: 'service_time', title: '服务日期',},
                        {
                            field: 'status', title: __('服务状态'),
                            // searchList: {
                            //     "1": __('开启'),
                            //     "0": __('关闭'),
                            // },
                            formatter: function (value) {
                                switch (value) {
                                    case 1:
                                        value = '待服务';
                                        break;
                                    case 2:
                                        value = '服务中';
                                        break;
                                    case 3:
                                        value = '服务完成-待评价';
                                        break;
                                    case 4:
                                        value = '服务完成-已评价';
                                        break;
                                    case 5:
                                        value = '售后';
                                        break;
                                }
                                return value;
                            }
                        },
                        {
                            field: 'create_time',
                            title: __('清洁结束时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {
                            field: 'operate', title: __('Operate'), table: table, buttons: [
                                {
                                    name: 'detail',
                                    text: __('查看详情'),
                                    title: __('查看详情'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-dialog',
                                    icon: 'fa fa-eye',
                                    url: 'check/order/service_detail?id={id}',
                                    // extend:'data-area=["60%","70%"]',
                                    callback: function (data) {
                                    },
                                },
                                {
                                    name: 'after_sale',
                                    text: __('申请售后'),
                                    title: __('申请售后'),
                                    classname: 'btn btn-xs btn-warning btn-postpone btn-magic btn-dialog',
                                    icon: 'fa fa-paint-brush',
                                    url: 'check/order/service_after_sale?id={id}',
                                    // extend:'data-area=["60%","70%"]',
                                    callback: function (data) {
                                    },
                                },
                            ], events: Table.api.events.operate, formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                $(table).data("operate-edit", false); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                $(table).data("operate-del", false); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        },
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        //服务单详情
        service_detail: function () {
            Controller.api.bindevent();
        },
        //申请售后
        service_after_sale: function () {
            Controller.api.bindevent();
        },

        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});

function select_school_class() {
    var that = this;
    var opt = $("select[name='row[user_type]']").val();
    var multiple = $(this).data("multiple") ? $(this).data("multiple") : false;
    var mimetype = $(this).data("mimetype") ? opt : 0;
    var admin_id = $(this).data("admin-id") ? $(this).data("admin-id") : '';
    var user_id = $(this).data("user-id") ? $(this).data("user-id") : '';
    var teacher_id = $("#teacher_id").val();
    var school_id = $("#school_id").val();

    parent.Fast.api.open("trusteeship/my_class/select_school_class?element_id=" + teacher_id + "&school_id=" + school_id + "&multiple=" + multiple + "&mimetype=" + mimetype + "&admin_id=" + admin_id + "&user_id=" + user_id, __('Choose'), {
        callback: function (data) {
            console.log(data.id)
            console.log(data.name)
            $("#school_class_id").val(data.id)
            $("#school_class").val(data.name)
        }
    });
}

function select_teacher() {
    var that = this;
    var opt = $("select[name='row[user_type]']").val();
    var multiple = $(this).data("multiple") ? $(this).data("multiple") : false;
    var mimetype = $(this).data("mimetype") ? opt : 0;
    var admin_id = $(this).data("admin-id") ? $(this).data("admin-id") : '';
    var user_id = $(this).data("user-id") ? $(this).data("user-id") : '';
    var teacher_id = $("#teacher_id").val();
    var school_id = $("#school_id").val();

    parent.Fast.api.open("trusteeship/my_class/select_teacher?element_id=" + teacher_id + "&school_id=" + school_id + "&multiple=" + multiple + "&mimetype=" + mimetype + "&admin_id=" + admin_id + "&user_id=" + user_id, __('Choose'), {
        callback: function (data) {
            console.log(data.id)
            console.log(data.name)
            $("#teacher_id").val(data.id)
            $("#teacher").val(data.name)
        }
    });
}

function select_class_room() {
    var that = this;
    var opt = $("select[name='row[user_type]']").val();
    var multiple = $(this).data("multiple") ? $(this).data("multiple") : false;
    var mimetype = $(this).data("mimetype") ? opt : 0;
    var admin_id = $(this).data("admin-id") ? $(this).data("admin-id") : '';
    var user_id = $(this).data("user-id") ? $(this).data("user-id") : '';
    var teacher_id = $("#teacher_id").val();
    var school_id = $("#school_id").val();
    if (!$("#plan_name").val()) {
        $("#store_id").val(0)
        store_id = 0;
    }
    parent.Fast.api.open("trusteeship/my_class/select_class_room?element_id=" + teacher_id + "&school_id=" + school_id + "&multiple=" + multiple + "&mimetype=" + mimetype + "&admin_id=" + admin_id + "&user_id=" + user_id, __('Choose'), {
        callback: function (data) {
            console.log(data.id)
            console.log(data.name)
            $("#classroom_id").val(data.id)
            $("#classroom").val(data.name)
        }
    });
}

function select_student() {
    var that = this;
    var opt = $("select[name='row[user_type]']").val();
    var multiple = $(this).data("multiple") ? $(this).data("multiple") : false;
    var mimetype = $(this).data("mimetype") ? opt : 0;
    var admin_id = $(this).data("admin-id") ? $(this).data("admin-id") : '';
    var user_id = $(this).data("user-id") ? $(this).data("user-id") : '';
    var teacher_id = $("#teacher_id").val();
    var school_id = $("#school_id").val();
    parent.Fast.api.open("reception/sheet/select_student?element_id=" + teacher_id + "&school_id=" + school_id + "&multiple=" + multiple + "&mimetype=" + mimetype + "&admin_id=" + admin_id + "&user_id=" + user_id, __('Choose'), {
        callback: function (data) {
            console.log(data.id);
            console.log(data.name);
            $("#student_id").val(data.id);
            $("#student_name").val(data.student_name);
            $("#student_name1").val(data.student_name);
            $("#student_name2").val(data.student_name);
            $("#c-image").val(data.image);
            $("#phone").val(data.phone);
            $("#parents").val(data.parents);
            $("#school_class_id").val(data.school_class_id);
            $("#school_class").val(data.school_class);
        }
    });
}